package cn.gov.anhai.controller;

import cn.gov.anhai.filter.Auth;
import cn.gov.anhai.service.DynamicService;
import cn.gov.anhai.service.TourismService;
import cn.gov.anhai.utils.ApiResult;
import cn.gov.anhai.utils.LoginUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"全域旅游"})
public class TourismController {
    private Logger logger = LoggerFactory.getLogger(TourismController.class);

    @Resource
    TourismService tourismService;

    @Resource
    DynamicService dynamicService;

    /**
     * 搜索
     */
    @GetMapping(value = "/search/list")
    @ApiOperation(value = "搜索")
    public ApiResult<Object> searchList(Integer current, Integer size, String name, HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> records = tourismService.list(current, size, null, name, null, null, null);
        Integer total = tourismService.listCount(null, name, null, null, null);
        result.put("records", records);
        result.put("total", total);
        return ApiResult.ok(result);
    }

    /**
     * 列表
     */
    @GetMapping(value = "/map/tourism/list")
    @ApiOperation(value = "列表")
    public ApiResult<Object> mapTourismList(Integer current, Integer size, Long type, HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> records = tourismService.list(current, size, type, null, null, null, null);
        Integer total = tourismService.listCount(type, null, null, null, null);
        result.put("records", records);
        result.put("total", total);
        return ApiResult.ok(result);
    }

    /**
     * 景点详情
     */
    @GetMapping(value = {"/tourism/{id}", "/tourism/way/{id}"})
    @ApiOperation(value = "景点详情")
    public ApiResult<Object> info(@PathVariable("id") Long id, HttpServletRequest request) throws Exception {
        Map<String, Object> info = tourismService.info(id, LoginUtil.getToken(request));
        if (info == null) return ApiResult.fail("查无信息");
        return ApiResult.ok(info);
    }

    /**
     * 所有标记类型
     */
    @GetMapping(value = "/map/tourism/maker-type")
    @ApiOperation(value = "所有标记类型")
    public ApiResult<Object> makerType(HttpServletRequest request) throws Exception {
        List<Map<String, Object>> list = tourismService.makerType();
        return ApiResult.ok(list);
    }

    /**
     * 所有标记
     */
    @GetMapping(value = "/map/tourism/maker")
    @ApiOperation(value = "所有标记")
    public ApiResult<Object> maker(HttpServletRequest request) throws Exception {
        List<Map<String, Object>> list = tourismService.maker();
        return ApiResult.ok(list);
    }

    /**
     * 收藏文旅信息
     */
    @Auth
    @PostMapping(value = "/tourism/collect/{id}")
    @ApiOperation(value = "收藏文旅信息")
    public ApiResult<Object> collect(@PathVariable("id") Long id, HttpServletRequest request) throws Exception {
        Map<String, Object> result = tourismService.collect(LoginUtil.getToken(request), id);
        if (result == null) return ApiResult.fail("收藏失败");
        return ApiResult.ok(result);
    }

    /**
     * 景点相关动态
     */
    @GetMapping(value = "/tourism/recommend")
    @ApiOperation(value = "景点相关动态")
    public ApiResult<Object> list(Integer current, Integer size, Long tourismId, HttpServletRequest request) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> records = dynamicService.list(current, size, tourismId, null, null, 0, 2, "desc");
        Integer total = dynamicService.listCount(tourismId, null, null, 0, 2);
        result.put("records", records);
        result.put("total", total);
        return ApiResult.ok(result);
    }
}
